const install = (Vue, vm) => {
  Vue.prototype.$tools = {}
  //显示toast
  Vue.prototype.$tools.showToast = (toast) => {
    uni.showToast({title: toast, icon: 'none'})
  }
  //显示loading
  Vue.prototype.$tools.showLoading = (title = '加载中...') => {
    uni.showLoading({title, mask: true})
  }
  //路由调整
  Vue.prototype.$tools.pageGo = (url) => {
    uni.navigateTo({url})
  }
  //路由延迟跳转-0.7s之后。
  Vue.prototype.$tools.pageGoDelay = (url, toast, delay = 500) => {
    if (toast) uni.showToast({title: toast, icon: 'none'})
    setTimeout(() => uni.navigateTo({url}), delay)
  }
  //路由延迟跳转-0.7s之后。
  Vue.prototype.$tools.pageGoRedirectDelay = (url, toast, delay = 500) => {
    if (toast) uni.showToast({title: toast, icon: 'none'})
    setTimeout(() => uni.redirectTo({url}), delay)
  }
  //延迟返回-如果有提示则提示toast
  Vue.prototype.$tools.pageBackDelay = (toast, delay = 700) => {
    if (toast) uni.showToast({title: toast, icon: 'none'})
    setTimeout(() => uni.navigateBack(), delay)
  }
  //页面数据回调：回调页面的路径、回调页面的data对应的key、回调key对应的value
  Vue.prototype.$tools.pageDataBack = (pageRoute, dataKey, value) => {
    let pages = getCurrentPages(), targetPage = pages.find(page => page.route === pageRoute)
    if (targetPage) targetPage.$vm[dataKey] = value;//重点$vm
  }
  //页面数据回调：回调页面的路径、回调页面的data对应的Obj的key，回调Obj多个值
  Vue.prototype.$tools.pageDataObjBack = (pageRoute, ObjKey, valueObj) => {
    let pages = getCurrentPages(), targetPage = pages.find(page => page.route === pageRoute)
    if (targetPage) targetPage.$vm[ObjKey] = {...targetPage.$vm[ObjKey], ...valueObj};
  }
  //页面方法回调：回调页面的路径、回调页面的方法、回调页面的参数
  Vue.prototype.$tools.pageCallBack = (pageRoute, call, params) => {
    let pages = getCurrentPages(), targetPage = pages.find(page => pageRoute.indexOf(page.route)!=-1)
    if (targetPage) {
      targetPage.$vm[call](params);//重点$vm
      vm.$tools.pageBackDelay('', 0)
    }
  }
  //页面数据回调延迟-0.5s之后。参数同上
  Vue.prototype.$tools.pageCallBackDelay = (pageRoute, call, params, toast) => {
    vm.$tools.showToast(toast)
    setTimeout(() => vm.$tools.pageCallBack(pageRoute, call, params), 500)
  }
  //显示文本弹窗并复制
  Vue.prototype.$tools.showCopyModal = (content) => {
    uni.showModal({content, success: (res) => uni.setClipboardData({data: content})})
  }
  //显示文本弹窗
  Vue.prototype.$tools.showModal = (content) => {
    uni.showModal({content,showCancel: false,})
  }
}
export default {
  install
}
